home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3 / CHAPTER7 / EXTFETCH.C < prev    next >
C/C++ Source or Header  |  1996-04-28  |  11KB  |  310 lines

  1.  
  2. #include <windows.h>  
  3. #include "extfetch.h"
  4. #include "sqlext.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "SQLExtendedFetch()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  107. {
  108. static HENV  hEnv  = NULL;
  109. static HDBC  hDBC  = NULL;
  110. static HWND  hList = NULL;
  111. UCHAR  szSqlStr[]  = "Select dept_id, dept_name, dept_head_id From department";
  112.  
  113.    switch( uMsg )
  114.    {
  115.       case WM_CREATE :
  116.               {
  117.                  RETCODE retCode;
  118.  
  119.                  // Allocate Environment and Connection.
  120.                  //.....................................
  121.                  SQLAllocEnv( &hEnv );
  122.                  SQLAllocConnect( hEnv, &hDBC );
  123.  
  124.                  // Connect to the data source.
  125.                  //............................
  126.                  retCode = SQLConnect( hDBC, 
  127.                              "Test Data Source", SQL_NTS,
  128.                              "DBA",              SQL_NTS,  
  129.                              "SQL",              SQL_NTS );
  130.  
  131.                  if ( retCode != SQL_SUCCESS )
  132.                  {
  133.                     SQLFreeConnect( hDBC );
  134.                     SQLFreeEnv( hEnv );
  135.                     return( -1 );
  136.                  }
  137.  
  138.                  if ( retCode == SQL_SUCCESS )
  139.                  {
  140.                     int nTabSize = 40;
  141.  
  142.                     hList = CreateWindow( "LISTBOX", "",    
  143.                                LBS_NOTIFY | LBS_USETABSTOPS |
  144.                                LBS_NOINTEGRALHEIGHT  | 
  145.                                WS_BORDER  | WS_CHILD | 
  146.                                WS_VISIBLE | WS_VSCROLL, 
  147.                                0, 0, 0, 0, 
  148.                                hWnd, (HMENU)101,
  149.                                hInst, NULL );
  150.                     
  151.                     SendMessage( hList, LB_SETTABSTOPS, 
  152.                                  1, (LPARAM)&nTabSize );
  153.                  }
  154.               }
  155.               break;
  156.  
  157.       case WM_SIZE :
  158.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  159.                                        HIWORD( lParam ), TRUE );
  160.               break; 
  161.               
  162.       case WM_COMMAND :
  163.               switch( LOWORD( wParam ) )
  164.               {
  165.                  case IDM_TEST :
  166.                         {
  167.                            HSTMT   hStmt;
  168.                            RETCODE retCode;
  169.  
  170.                            // Allocate a statement handle for the query.
  171.                            // ..........................................
  172.                            SQLAllocStmt( hDBC, &hStmt );
  173.  
  174.                            // Use a dynamic cursor so that extended 
  175.                            // fetch capabilities may be utilized.
  176.                            // .....................................
  177.                            retCode = SQLSetStmtOption( hStmt, 
  178.                                                        SQL_CURSOR_TYPE,
  179.                                                        SQL_CURSOR_DYNAMIC );
  180.  
  181.                            // Call SQLPrepare() to compile the query.
  182.                            // .......................................
  183.                            retCode = SQLPrepare( hStmt, szSqlStr, 
  184.                                                  sizeof(szSqlStr) );
  185.  
  186.                            // Call SQLExecute() to execute the query.
  187.                            // .......................................
  188.                            if( retCode == SQL_SUCCESS || 
  189.                                retCode == SQL_SUCCESS_WITH_INFO )
  190.                            {
  191.                               int     irow = 3;
  192.                               int     crow;
  193.                               UWORD   rgfRowStatus;
  194.                               UCHAR   szDeptId    [32];
  195.                               UCHAR   szDeptName  [32];
  196.                               UCHAR   szDeptHeadId[32];
  197.                               UCHAR   szLBStr[128];
  198.                               SDWORD  dwTypeLen;
  199.  
  200.                               retCode = SQLExecute( hStmt );
  201.  
  202.                               if( retCode == SQL_SUCCESS || 
  203.                                   retCode == SQL_SUCCESS_WITH_INFO )
  204.                               {
  205.  
  206.                                  // Bind a column to the dept_id, 
  207.                                  // dept_name, and dept_head_id 
  208.                                  // columns in the result set.
  209.                                  // .............................
  210.  
  211.                                  SQLBindCol( hStmt, 1, SQL_C_CHAR,
  212.                                     szDeptId, sizeof( szDeptId ), 
  213.                                     &dwTypeLen );
  214.  
  215.                                  SQLBindCol( hStmt, 2, SQL_C_CHAR, 
  216.                                     szDeptName, sizeof( szDeptName ), 
  217.                                     &dwTypeLen );
  218.  
  219.                                  SQLBindCol( hStmt, 3, SQL_C_CHAR, 
  220.                                     szDeptHeadId, sizeof( szDeptHeadId ), 
  221.                                     &dwTypeLen );
  222.  
  223.                                  // Call SQLExtendedFetch() to return 
  224.                                  // data starting with the third row 
  225.                                  // in the rowset, and put the data
  226.                                  // in the list box.
  227.                                  // .................................
  228.  
  229.                                  retCode = SQLExtendedFetch( hStmt, 
  230.                                     SQL_FETCH_ABSOLUTE, irow++, &crow, 
  231.                                     &rgfRowStatus );
  232.  
  233.                                  while ( retCode == SQL_SUCCESS )
  234.                                  {
  235.                                     szLBStr[0] = '\0';
  236.                                     strcat( szLBStr, szDeptId );
  237.                                     strcat( szLBStr, "\t" );
  238.                                     strcat( szLBStr, szDeptName );
  239.                                     strcat( szLBStr, "\t" );
  240.                                     strcat( szLBStr, szDeptHeadId );
  241.  
  242.                                     SendMessage( hList, LB_ADDSTRING, 
  243.                                                  0, (LPARAM)szLBStr );
  244.  
  245.                                     retCode = SQLExtendedFetch( hStmt, 
  246.                                        SQL_FETCH_ABSOLUTE, irow++, &crow, 
  247.                                        &rgfRowStatus );
  248.                                  }
  249.                               }
  250.                            }
  251.  
  252.                            // Free the statement handle.
  253.                            // ..........................
  254.                            SQLFreeStmt( hStmt, SQL_DROP );
  255.                         }
  256.                         break;
  257.  
  258.                  case IDM_ABOUT :
  259.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  260.                         break;
  261.  
  262.                  case IDM_EXIT :
  263.                         DestroyWindow( hWnd );
  264.                         break;
  265.               }
  266.               break;
  267.       
  268.       case WM_DESTROY :
  269.               PostQuitMessage(0);
  270.  
  271.               // Disconnect Environment.
  272.               //........................
  273.               SQLDisconnect( hDBC );
  274.  
  275.               // Free Connection and Environment.
  276.               //.................................
  277.               SQLFreeConnect( hDBC );
  278.               SQLFreeEnv( hEnv );
  279.               break;
  280.  
  281.       default :
  282.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  283.    }
  284.  
  285.    return( 0L );
  286. }
  287.  
  288. LRESULT CALLBACK About( HWND hDlg,           
  289.                         UINT message,        
  290.                         WPARAM wParam,       
  291.                         LPARAM lParam)
  292. {
  293.    switch (message) 
  294.    {
  295.        case WM_INITDIALOG: 
  296.                return (TRUE);
  297.  
  298.        case WM_COMMAND:                              
  299.                if (   LOWORD(wParam) == IDOK         
  300.                    || LOWORD(wParam) == IDCANCEL)    
  301.                {
  302.                        EndDialog(hDlg, TRUE);        
  303.                        return (TRUE);
  304.                }
  305.                break;
  306.    }
  307.  
  308.    return (FALSE); 
  309. }
  310.